package swd.basic.table_schema_maintain;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
   QueryBean gqbnDAO=null;
   public String action(javax.servlet.jsp.PageContext pageContext){
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
      HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

      View view=(View)request.getAttribute("view");
      (new WebFunction()).setPortal_to_session(request); //set session from portal
      if (request.getSession().getAttribute("u_id")==null) {
         try{
            request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
            return "login";
         }
         catch(Exception le){
            view.setOnloadScript("can not redirect to login page...please contact IT");
            return "";
         }
      }
      //String lstrUserNO = request.getSession().getAttribute("u_id").toString();
      gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

      DataView DataView=null;
      DataViewForm DataViewForm=null;

      DataView = new DataView(view.getDBAlias(),view.getDBDebug(), view.getSourceCharset(), view.getTargetCharset());
      DataViewForm = new DataViewForm();

      request.setAttribute("DataView",DataView);//讓前端可以呼叫dataview
      request.setAttribute("DataViewForm",DataViewForm);


      Business business=new Business(gqbnDAO);
      //Flow flow=new Flow(gqbnDAO,lstrUserNO,"SWD",view,business);

      //String lstrResult=null;

      try{

         view.setinsertcount("0");
         view.setButtonVisible("0");
         view.setTipMessage("定義table內的欄位設定。請輸入表格名稱或選取已存在表格名稱後再按【新增】鍵");
         if(view.getPrgAction()!=null && view.getPrgAction().equals("query")){
            view.setTipMessage("定義好欄位資訊後再按【 整批存檔】");
            view.setdataviewtotalcount("0");
            view.setdbf_no_readonly(false);
            view.setPrgStatus("query");
            view.setButtonVisible("3");
         }


         if(view.getPrgAction()!=null && view.getPrgAction().equals("add")){
            //flow.preAddAction(request.getSession().getAttribute("u_id").toString());
            view.setTipMessage("若欲新增資料列請按【新增】鈕，定義好欄位資訊後再按【 整批存檔】");
            view.setPrgStatus("add");
             view.setButtonVisible("1");
         }

         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("add")){
            //view.setOnloadScript("alert('" + flow.addAction() + "');");
            view.setBtnQueryShow("");
         }

         //按整批存檔
          if(view.getPrgAction()!=null && view.getPrgAction().equals("saveall")){
              if(view.getrowsn()==null || view.getrowsn().equals("")){
                  view.setOnloadScript("alert('沒有修改!');");
              } else{
                   view.setOnloadScript("alert('"+business.batchUpdateData(view)+"');");
                   view.setdataviewtotalcount("0");
                   view.setdbf_no_readonly(false);
              }
              view.setPrgStatus("save");
              view.setBtnQueryShow("");
              view.setButtonVisible("3");
          }

          //按刪除
          if(view.getPrgAction()!=null && view.getPrgAction().equals("delete")){
             //刪除之前先存檔
             business.saveAllData(view);
             business.exeDelSQL(view,Integer.parseInt(view.getrowsn()));
             view.setdataviewtotalcount(String.valueOf (Integer.parseInt(view.getdataviewtotalcount())-1));
             view.setBtnQueryShow("none");
          }


         // 離開
         if(view.getPrgAction()!=null && view.getPrgAction().equals("exit")){
             response.sendRedirect("table_schema_maintain.jsp");
             return "";
         }

         business.setDataViewForm(DataViewForm,view,request.getSession().getAttribute("u_id").toString());
         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT.');");
         return "";
      }
      finally{
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
